#include "test_mode.h"
#include "my_define.h"
#include "pir_sensor_detect.h"
#include "key.h"
#include "systime.h"
#include "relay.h"
#include "led.h"
#include "qDebug.h"

#define PIR_OFF_FILTER_TIME     2500 //单位ms 继电器关闭后的PIR滤波时间
#define KEY_OFF_SKIP_TIME     3     //单位s 按键关闭后PIR失效时间
#define PIR_UNOCCUPIED_TIME    3 //检测连续无人关闭负载得时间


typedef enum
{
    MODE_INIT,
    MODE_START,
    MODE_OFF_FILTER,
    MODE_RELAY_OFF,
    MODE_RELAY_ON,
    MODE_PIR_OFF_STATGE,
}MODE_STATE_E;

static struct
{
   MODE_STATE_E     mode_state; 
   timestamp_t           mode_rtc;

}mode_struct;




void test_mode_init(void)
{
    mode_struct.mode_state = MODE_INIT;
}



void test_mode_task(void)
{
    switch(mode_struct.mode_state){
        case MODE_INIT:
//            qDebug(0x01);
            set_key_switch(KEY_DISABLE);
            pir_sensor_set_status(PIR_SENSOR_DISABLED);
            set_relay_switch(ON);
            set_led(ON);
            relative_time_add_sec(&mode_struct.mode_rtc,30);
            mode_struct.mode_state = MODE_START;
            break;
        case MODE_START:
            if(is_relative_time_timeout(&mode_struct.mode_rtc)){
//                qDebug(0x02);
                
                set_key_switch(KEY_ENABLE);
                
                set_relay_switch(OFF);
                set_led(OFF);
//                mode_struct.mode_state = MODE_RELAY_OFF;
                pir_sensor_set_status(PIR_SENSOR_DISABLED);
                relative_time_add_ms(&mode_struct.mode_rtc,PIR_OFF_FILTER_TIME);
                mode_struct.mode_state = MODE_OFF_FILTER;
            }
            break;
        case MODE_OFF_FILTER:
            if(is_relative_time_timeout(&mode_struct.mode_rtc)){
//                qDebug(0x03);
                pir_sensor_set_status(PIR_SENSOR_ENABLED);
                mode_struct.mode_state = MODE_RELAY_OFF;
            }else if(is_key_click()){
//                qDebug(0x04);
                pir_sensor_set_status(PIR_SENSOR_ENABLED);
                mode_struct.mode_state = MODE_RELAY_OFF;
            }

            break;
        case MODE_RELAY_OFF:
            if(is_key_click()){
//                qDebug(0x05);
                
                set_relay_switch(ON);
                set_led(ON);
                mode_struct.mode_state = MODE_RELAY_ON;
            }else if(pir_sensor_is_occupied()){
//                qDebug(0x06);
                set_relay_switch(ON);
                set_led(ON);
                mode_struct.mode_state = MODE_RELAY_ON;
            }
            break;
        case MODE_RELAY_ON:
             if(is_key_click()){
//                qDebug(0x07);
                
                set_relay_switch(OFF);
                set_led(OFF);
                 
                pir_sensor_set_status(PIR_SENSOR_DISABLED); 
                relative_time_add_sec(&mode_struct.mode_rtc,KEY_OFF_SKIP_TIME);
//                mode_struct.mode_state = MODE_KEY_OFF_STATGE;
                 mode_struct.mode_state = MODE_OFF_FILTER;
             }else if(!pir_sensor_is_occupied()){
//                qDebug(0x08);
                relative_time_add_sec(&mode_struct.mode_rtc,PIR_UNOCCUPIED_TIME);
                mode_struct.mode_state = MODE_PIR_OFF_STATGE;
             }
            break;
        case MODE_PIR_OFF_STATGE:
            if(is_relative_time_timeout(&mode_struct.mode_rtc)){
//                qDebug(0x09);
                set_relay_switch(OFF);
                set_led(OFF);
//                mode_struct.mode_state = MODE_RELAY_OFF;
                pir_sensor_set_status(PIR_SENSOR_DISABLED);
                relative_time_add_ms(&mode_struct.mode_rtc,PIR_OFF_FILTER_TIME);
                mode_struct.mode_state = MODE_OFF_FILTER;
            }else if(pir_sensor_is_occupied()){
//                qDebug(0x0A);
                mode_struct.mode_state = MODE_RELAY_ON;
            }else if(is_key_click()){
//               qDebug(0x0B);
               mode_struct.mode_state = MODE_RELAY_ON; 
            }
            break;
        default:
            break;
    }

}


